home *** CD-ROM | disk | FTP | other *** search
/ Practical Algorithms for Image Analysis / Practical Algorithms for Image Analysis.iso / GD / gd1_2_tar.z / gd1_2_tar / gd1.2 / webgif.c < prev   
Encoding:
C/C++ Source or Header  |  1995-08-08  |  4.1 KB  |  165 lines

  1. /* Bring in the gd library functions */
  2. #include "gd.h"
  3.  
  4. /* Bring in standard I/O and string manipulation functions */
  5. #include <stdio.h>
  6. #include <string.h>
  7.  
  8. int main(int argc, char **argv)
  9. {
  10.     FILE *in;
  11.     FILE *out;
  12.     /* Declare our image pointer */
  13.     gdImagePtr im = 0;
  14.     int i;
  15.        /* We'll clear 'no' once we know the user has made a
  16.         reasonable request. */
  17.     int no = 1;
  18.     /* We'll set 'write' once we know the user's request
  19.         requires that the image be written back to disk. */
  20.     int write = 0;
  21.     /* C programs always get at least one argument; we want at
  22.         least one more (the image), more in practice. */
  23.     if (argc < 2) {
  24.         no = 1;    
  25.         goto usage;
  26.     }
  27.     /* The last argument should be the image. Open the file. */
  28.     in = fopen(argv[argc-1], "rb");    
  29.     if (!in) {
  30.         fprintf(stderr,
  31.             "Error: can't open file %s.\n", argv[argc-1]);
  32.     }
  33.     /* Now load the image. */    
  34.     im = gdImageCreateFromGif(in);
  35.     fclose(in);
  36.     /* If the load failed, it must not be a GIF file. */
  37.     if (!im) {
  38.         fprintf(stderr,
  39.             "Error: %s is not a valid gif file.\n", argv[1]);
  40.         exit(1);    
  41.     }
  42.     /* Consider each argument in turn. */
  43.     for (i=1; (i < (argc-1)); i++) {
  44.         /* -i turns on and off interlacing. */
  45.         if (!strcmp(argv[i], "-i")) {
  46.             if (i == (argc-2)) {
  47.                 fprintf(stderr, 
  48.                 "Error: -i specified without y or n.\n");
  49.                 no = 1;
  50.                 goto usage;
  51.             }
  52.             if (!strcmp(argv[i+1], "y")) {
  53.                 /* Set interlace. */
  54.                 gdImageInterlace(im, 1);
  55.             } else if (!strcmp(argv[i+1], "n")) {
  56.                 /* Clear interlace. */
  57.                 gdImageInterlace(im, 0);
  58.             } else {
  59.                 fprintf(stderr,
  60.                 "Error: -i specified without y or n.\n");
  61.                 no = 1;
  62.                 goto usage;
  63.             }
  64.             i++;
  65.             no = 0;
  66.             write = 1;
  67.         } else if (!strcmp(argv[i], "-t")) {
  68.             /* Set transparent index (or none). */
  69.             int index;
  70.             if (i == (argc-2)) {
  71.                 fprintf(stderr,
  72.         "Error: -t specified without a color table index.\n");
  73.                 no = 1;
  74.                 goto usage;
  75.             }
  76.             if (!strcmp(argv[i+1], "none")) {
  77.                 /* -1 means not transparent. */
  78.                 gdImageColorTransparent(im, -1);
  79.             } else {
  80.                 /* OK, get an integer and set the index. */
  81.                 index = atoi(argv[i+1]);
  82.                 gdImageColorTransparent(im, index);
  83.             }
  84.             i++;
  85.             write = 1;
  86.             no = 0;
  87.         } else if (!strcmp(argv[i], "-l")) {
  88.             /* List the colors in the color table. */
  89.             int j;
  90.             /* Tabs used below. */
  91.             printf("Index    Red    Green    Blue\n");
  92.             for (j=0; (j < gdImageColorsTotal(im)); j++) {
  93.                 /* Use access macros to learn colors. */
  94.                 printf("%d    %d    %d    %d\n",
  95.                     j, 
  96.                     gdImageRed(im, j),
  97.                     gdImageGreen(im, j),
  98.                     gdImageBlue(im, j));
  99.             }
  100.             no = 0;
  101.         } else if (!strcmp(argv[i], "-d")) {
  102.             /* Output dimensions, etc. */
  103.             int t;
  104.             printf("Width: %d Height: %d Colors: %d\n",
  105.                 gdImageSX(im), gdImageSY(im),
  106.                 gdImageColorsTotal(im));
  107.             t = gdImageGetTransparent(im);
  108.             if (t != (-1)) {
  109.                 printf("Transparent index: %d\n", t);
  110.             } else {
  111.                 /* -1 means the image is not transparent. */
  112.                 printf("Transparent index: none\n");
  113.             }
  114.             if (gdImageGetInterlaced(im)) {
  115.                 printf("Interlaced: yes\n");    
  116.             } else {
  117.                 printf("Interlaced: no\n");    
  118.             }
  119.             no = 0;
  120.         } else {
  121.             fprintf(stderr, "Unknown argument: %s\n", argv[i]);
  122.             break;    
  123.         }
  124.     }
  125. usage:
  126.     if (no) {
  127.         /* If the command failed, output an explanation. */
  128.         fprintf(stderr, 
  129.     "Usage: webgif [-i y|n ] [-l] [-t index|off ] [-d] gifname.gif\n");
  130.         fprintf(stderr, 
  131.     "Where -i controls interlace (specify y or n for yes or no),\n");
  132.         fprintf(stderr, 
  133.     "-l outputs a table of color indexes, -t sets the specified\n");
  134.         fprintf(stderr, 
  135.     "color index (0-255 or none) to be the transparent color, and\n");
  136.         fprintf(stderr,
  137.     "-d reports the dimensions and other characteristics of the image.\n");
  138.         fprintf(stderr, 
  139.     "Note: you may wish to pipe to \"more\" when using the -l option.\n");
  140.     } 
  141.     if (write) {
  142.         /* Open a temporary file. */
  143.         out = fopen("temp.tmp", "wb");
  144.         if (!out) {
  145.             fprintf(stderr,
  146.                 "Unable to write to temp.tmp -- exiting\n");
  147.             exit(1);
  148.         }
  149.         /* Write the new gif. */
  150.         gdImageGif(im, out);
  151.         fclose(out);
  152.         /* Erase the old gif. */
  153.         unlink(argv[argc-1]);
  154.         /* Rename the new to the old. */
  155.         rename("temp.tmp", argv[argc-1]);
  156.     }
  157.     /* Delete the image from memory. */
  158.     if (im) {
  159.         gdImageDestroy(im);
  160.     }
  161.     /* All's well that ends well. */
  162.     return 0;
  163. }
  164.  
  165.